
local ui = class.select_panel.create('玩家-选择抽卡')
ui:set_select_button(3,class.card_data,300,429)
ui:hide()

for a,button in ipairs(ui.childs) do
    local x1,y1 = button:get_real_position()
    local y2 = y1 + (button.index%2==1 and 1 or -1)*200
    button.y1 = y1
    button.y2 = y2
end

--重置按钮
local refresh = ui:add_namebutton('刷新卡牌',(1920-300)/2,660,150,60,12,5)
ac.card_refresh = refresh

function refresh:on_button_clicked()
    ac.sync('玩家-刷新抽卡')
end


--关闭
local esc = ui:add_namebutton('待会再选',(1920-300)/2 + 180,660,150,60,12,5)

function esc:on_button_clicked()
    ui:hide()
end



function ui:update()
    local player = ac.player.self
    local hero = player.hero
    --动画
    local time = (self.wait_select - ac.clock()) / 1000
    if time>0 then
        for a,button in ipairs(self.childs) do
            if button:is_show() then
                button:set_real_position(button.x,button.y2)
                button:add_ani_tween({button.x,button.y1},time)
            end
        end
    end
    if hero then
        local num = hero:get_data('免费刷新',0)
        local pay = player:get_data('刷新次数',0)*200 + 200
        if num>0 then
            refresh.text:set_text(('免费刷新(|cff00ff00%0.f|r)'):format(num))
        elseif pay>0 then
            refresh.text:set_text(('刷新(|cffff0000%0.f|r杀敌)'):format(pay))
        else
            refresh.text:set_text(('刷新'):format(num))
        end

    end
end


function ui:on_button_clicked(button)
    if button.index and button.button_type=='选择按钮' and self.wait_select<ac.clock() then
        local time = 0.15
        self.wait_select = ac.clock() + time * 1000
        ac.sync(self.sync_name or self.name,button.index,self.wait_select)

        --动画
        for a,button in ipairs(self.childs) do
            if button:is_show() then
                button:set_real_position(button.x,button.y1)
                button:add_ani_tween({button.x,button.y2},time)
            end
        end
        --运动动画
        local panel = ac.console.abilitydetail[3]
        local icon = button.icon:copy_temp_icon()
        local x,y = panel:get_real_position()
        icon:add_ani_tween({x,y,panel.w,panel.h},1,'线性')
    end 
end

ac.sync:event '玩家-选择抽卡'(function(_,player,index,time)
    local time = time - ac.clock()
    player._tf_ani_timer = ac.wait(time,function()
        player._tf_ani_timer = nil
        if player:is_self() and ui.wait_select <= ac.clock() then
            ui:hide()
        end
    end)
end)


ac.game:event '玩家-显示抽卡'(function(_,player,data,show)
    if player._tf_ani_timer then
        player._tf_ani_timer:remove()
        player._tf_ani_timer = nil
    end
    if player:is_self() then
        if show then
            ui:set_data(data,0.15)
        else
            if ui.show_flag then
                ui:hide()
            else
                ui:set_data(data)
            end
        end
    end
end)
